package com.qjj.studydainty.adapter;
/*
 * Title:       <br>
 * Description: <br>
 *
 * @author 曲建金
 * @version 1.0
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import com.qjj.studydainty.R;
import com.qjj.studydainty.entity.Marker;

import java.util.List;

public class MarkerAdapter extends RecyclerView.Adapter<MarkerViewHolder> {

    private LayoutInflater mInflater;
    private Context mContext;
    private List<Marker> mDatas;

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    private int position;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void OnItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public MarkerAdapter(Context context, List<Marker> datas, RecyclerView recyclerView) {
        this.mContext = context;
        this.mDatas = datas;
        mInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public MarkerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.marker_adapter, viewGroup, false);
        MarkerViewHolder markerViewholder = new MarkerViewHolder(view);
        return markerViewholder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MarkerViewHolder markerViewholder, final int position) {
        Marker marker = mDatas.get(position);
        markerViewholder.name_textView.setText(marker.getName());
        markerViewholder.url_textView.setText(marker.getUrl());

        setUpItemEvent(markerViewholder);
    }

    protected void setUpItemEvent(@NonNull final MarkerViewHolder markerViewholder) {
        if (onItemClickListener != null) {

            markerViewholder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = markerViewholder.getLayoutPosition();
                    onItemClickListener.onItemClick(markerViewholder.itemView, layoutPosition);
                }
            });
            markerViewholder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int layoutPosition = markerViewholder.getLayoutPosition();
                    setPosition(layoutPosition);
                    onItemClickListener.OnItemLongClick(markerViewholder.itemView, layoutPosition);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void addData(int position) {
        mDatas.add(position, null);
        notifyItemInserted(position);
    }

    public void deleteData(int position) {
        mDatas.remove(position);
        notifyItemRemoved(position);
    }
    public void deleteAllData() {
        int size = mDatas.size();
        mDatas.clear();
        notifyItemRangeRemoved(0, size);
    }


}

class MarkerViewHolder extends ViewHolder {
    TextView name_textView;
    TextView url_textView;

    public MarkerViewHolder(View view) {
        super(view);
        name_textView = view.findViewById(R.id.textView_name);
        url_textView = view.findViewById(R.id.textView_path_time);
    }
}
